<?php

namespace App\Http\Controllers\admin;

use App\Model\UserModel;
use App\Model\UserWishOrder;
use App\Model\WishModel;
use App\Model\SkuModel;
use App\Model\ShopGoodModel;
use Illuminate\Http\Request;
use App\Model\AdminLogsModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;


//主页
class WishController extends Controller
{
	//心愿列表
	public function index(Request $request){
		$list = UserWishOrder::where(function ($query) {
			//状态
			if (request()->state) {
				$query->where('state', request()->state);
			}
			//下单时间
			if (request()->month) {
				$month_arr = explode("~", request()->month);
				$mintime = $month_arr[0];
				$maxtime = $month_arr[1] . " 23:59:59";
				$query->whereBetween('created_at', [$mintime, $maxtime]);
			}
		})->orderBy('id','desc')->paginate(15);
		if($list){
			foreach($list as $k=>$v){
				$wish = WishModel::where('id',$v['wid'])->first();
				$v['gname'] = $wish['gname'];
				$v['total'] = $wish['total'];
				$v['in_total'] = $wish['in_total'];
				$v['user'] = UserModel::where('id',$v['uid'])->value('user');
			}
		}

		return view('admin.wish.index',compact('request','list'));
	}

	//加载发货界面
	public function send_ok(Request $request){
		$id = request()->id;
		return view('admin.wish.send_ok',compact('id','request'));
	}

	//发货
	public function send_ok_cy(){
		$id = request()->id;
		$express_num = request()->express_num;
		$express_com = request()->express_com;

		//心愿订单
		$wish_order = UserWishOrder::where('id',$id)->where('state',1)->first();
		if(!$wish_order){
			return ['code'=>0,'msg'=>'该订单有误'];
		}
		DB::beginTransaction();
		try{
			$user_wish = WishModel::where('id',$wish_order['wid'])->first();
			if($user_wish['sku_id'] > 0){
				$num = SkuModel::where('id',$user_wish['sku_id'])->value('sum');
			}else{
				$num = ShopGoodModel::where('id',$user_wish['gid'])->value('num');
			}
			
			if($num <= 0){
				return ['code'=>0,'msg'=>'库存不足'];
			}
			//更改心愿单状态：待收货
			UserWishOrder::where('id',$id)->update(['express_num'=>$express_num,'express_com'=>$express_com,'send_time'=>date('Y-m-d H:i:s'),'state'=>2]);
			//心愿信息
			$wish_list = WishModel::where('id',$wish_order['wid'])->first();
			//扣除库存 有规格
			if($wish_list['sku_id'] > 0){
				SkuModel::where('id',$wish_list['sku_id'])->decrement('sum');
				SkuModel::where('id',$wish_list['sku_id'])->increment('sale_num');
			}
			//扣除库存 没有规格
			ShopGoodModel::where('id',$wish_list['gid'])->decrement('num');
			ShopGoodModel::where('id',$wish_list['gid'])->increment('sale_num');
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'操作失败'];
		}
		return ['code'=>1,'msg'=>'已发货'];
	}

	//当前心愿进度
	public function list(Request $request){
		$list = WishModel::where(function ($query) {
			//状态
			if (request()->state) {
				$query->where('state', request()->state);
			}
			//下单时间
			if (request()->month) {
				$month_arr = explode("~", request()->month);
				$mintime = $month_arr[0];
				$maxtime = $month_arr[1] . " 23:59:59";
				$query->whereBetween('created_at', [$mintime, $maxtime]);
			}
		})->orderBy('id','desc')->paginate(15);
		foreach($list as $k=>$v){
			$has = '/upload/shop';
			if(strpos($v['picname'],$has) !== FALSE){
				$v['picname'] = 'http://'.$_SERVER['HTTP_HOST'].$v['picname'];
			}else{
				$v['picname'] = $v['picname'];
			}
			if(!$v['sku_attr']){
				$v['sku_attr'] = '无';
			}
		}
    
		return view('admin.wish.list',compact('list','request'));
	}

	
}